package com.baoyouqun.base.utils;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

/**
 * 文件类型工具类，用于判断文件是否可预览及预览方式
 */
public class FileTypeUtil {

    // 图片类型集合
    private static final Set<String> IMAGE_TYPES = new HashSet<>(Arrays.asList(
            "image/jpeg", "image/png", "image/gif", "image/bmp", "image/webp", "image/svg+xml"
    ));

    // 文本类型集合
    private static final Set<String> TEXT_TYPES = new HashSet<>(Arrays.asList(
            "text/plain", "text/html", "text/css", "text/javascript", "application/json"
    ));

    // PDF类型
    private static final String PDF_TYPE = "application/pdf";

    // Office文档类型
    private static final Set<String> OFFICE_TYPES = new HashSet<>(Arrays.asList(
            "application/msword", // doc
            "application/vnd.openxmlformats-officedocument.wordprocessingml.document", // docx
            "application/vnd.ms-excel", // xls
            "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", // xlsx
            "application/vnd.ms-powerpoint", // ppt
            "application/vnd.openxmlformats-officedocument.presentationml.presentation" // pptx
    ));

    /**
     * 判断是否为图片类型
     */
    public static boolean isImage(String contentType) {
        return contentType != null && IMAGE_TYPES.contains(contentType);
    }

    /**
     * 判断是否为文本类型
     */
    public static boolean isText(String contentType) {
        return contentType != null && TEXT_TYPES.contains(contentType);
    }

    /**
     * 判断是否为PDF类型
     */
    public static boolean isPdf(String contentType) {
        return PDF_TYPE.equals(contentType);
    }

    /**
     * 判断是否为Office文档类型
     */
    public static boolean isOfficeDocument(String contentType) {
        return contentType != null && OFFICE_TYPES.contains(contentType);
    }

    /**
     * 判断文件是否可预览
     */
    public static boolean canPreview(String contentType) {
        return isImage(contentType) || isText(contentType) || isPdf(contentType) || isOfficeDocument(contentType);
    }

    /**
     * 获取文件预览类型
     */
    public static String getPreviewType(String contentType) {
        if (isImage(contentType)) {
            return "image";
        } else if (isText(contentType)) {
            return "text";
        } else if (isPdf(contentType)) {
            return "pdf";
        } else if (isOfficeDocument(contentType)) {
            return "office";
        }
        return "unknown";
    }
}
    